package cn.tedu.ximart.server.web;


import cn.tedu.ximart.server.ex.ServiceException;
import lombok.Data;

import java.io.Serializable;

@Data
public class JsonResult<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;

    public static JsonResult ok(){
        JsonResult jsonResult = new JsonResult();
        jsonResult.code=ServiceCode.OK;
        return jsonResult;
    }

    public static<T> JsonResult<T> ok(T data){
        JsonResult<T> jsonResult = new JsonResult();
        jsonResult.code=ServiceCode.OK;
        jsonResult.data = data;
        return jsonResult;
    }

    public static JsonResult fail(ServiceException e){
        return fail(e.getServiceCode(), e.getMessage());
    }

    public static JsonResult fail(Integer code,String message){
        JsonResult jsonResult = new JsonResult();
        jsonResult.code=code;
        jsonResult.message = message;
        return jsonResult;
    }

}
